﻿var apiUrl = "../Api.ashx" + "?Random=" + Math.random(); //接口api地址
var g_webApiBaseUrl = "../api/etl/";
var loginUrl = "login.html";//登陆地址
var DeleteDataThenReload = false;//删除数据后是否刷新列表
var dataTableConfig = {
    "sProcessing": "正在加载数据...",
    "sZeroRecords": "没有您要搜索的内容",
    "sInfo": "从_START_ 到 _END_ /共 _TOTAL_ 条",
    "sInfoEmpty": "记录数为0",
    "sInfoFiltered": "(全部记录数 _MAX_  条)",
    "sInfoPostFix": "",
    "sUrl": "",
    "oPaginate": {
        "sFirst": "首页",
        "sPrevious": "上一页",
        "sNext": "下一页",
        "sLast": "末页"
    }
};

var dataTableListConfig = {
    "sProcessing": "正在加载数据...",
    "sZeroRecords": "没有您要搜索的内容",
    "sInfo": "",
    "sInfoEmpty": "",
    "sInfoFiltered": "",
    "sInfoPostFix": "",
    "sUrl": "",
    "oPaginate": {
        "sFirst": "首页",
        "sPrevious": "上一页",
        "sNext": "下一页",
        "sLast": "末页"
    }
};

//刷新的快捷键: ctrl+M
$(window).keydown(function (event) {
    if (event.ctrlKey && event.which == 77) {
        window.location.reload();
    }
});
var GetApiUrl = function () {
    return (apiUrl + "?Random=" + Math.random());
}
/*
*判断返回的错误代码是否为登陆状态
*2015-7-7 
* 邢博
*以登陆返回true 未登陆直接跳转至登陆页面
*/
var IsLogined = function (code) {
    var codelist = [65281, 65282, 65283, 65284, 65285, 65286,-1];
    var strlist = ["默认错误", " action not found", "无效的参数", "认证失败，没有认证信息", "  认证失败，已超时", "错误的HttpMethod"];
    if (code == 65284 || code == 65285) {
        // $("#Login-Modal").modal("show");
        // $.ajax({
        //    url: "AjaxLogin.html",
        //    success: function (data) {
        //        $("body").append(data);
        //        $("#Login-Modal").modal("show");
        //    }
        //});
        window.location.href = loginUrl + "?url=" + encodeURI(window.location.href);
    }
    else
        return true;
}
/*
*获取数据服务目录树 
*2015-7-7 
* 邢博
*返回目录树的数据
*/
var GetDataServiceFolderTree = function (parentId) {
    var list = "";
    $.ajax({
        url: apiUrl,
        type: "Get",
        dataType: "json",
        async: false,
        data: { "ParentId": parentId, "sys:sno": "ETL", "sys:act": "GetDataServiceFolderTree" },
        success: function (data) {
            if (data.IsSccuess) {
                if (data.Data.Items != null && data.Data.Items.length != 0) {
                    list += GetDataServiceFolderTreeChild(data.Data.Items, 0);
                }
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert("错误代码：" + data.ErrorCode + "，错误信息：" + data.Message);
                }
            }
        },
        error: function () {
            alert("系统异常，请稍后重试");
        }, beforeSend: function () {

        }
    });
    return list;
}
/*
*递归组合数据select 的option项
*2015-7-7 
* 邢博
*返回数据服务类型数据
*/
var GetDataServiceFolderTreeChild = function (source, i) {
    var str = "";
    for (var j = 0; j < source.length; j++) {
        str += ("<option value='" + source[j].Id + "'>");
        for (var k = 0; k < i; k++) {
            str += "&nbsp;&nbsp;";
        }
        str += (source[j].Caption + "(" + source[j].Code + ")<\/option>");
        if (source[j].Items != null && source[j].Itemslength != 0) {
            str += (GetDataServiceFolderTreeChild(source[j].Items, i + 1));
        }
    }
    return str;

}
/*
*获取数据服务类型列表 
*2015-7-7 
* 邢博
*返回数据服务类型数据
*/
var GetDataServiceTypeList = function () {
    var list = new Array();
    $.ajax({
        url: apiUrl,
        type: "Get",
        dataType: "json",
        async: false,
        data: { "sys:sno": "ETL", "sys:act": "GetDataServiceTypeList" },
        success: function (data) {
            if (data.IsSccuess) {
                for (var j = 0; j < data.Data.Categories.length; j++) {
                    for (var i = 0; i < data.Data.Categories[j].Items.length; i++) {
                        list.push(data.Data.Categories[j].Items[i]);
                    }
                }

            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }

        },
        error: function () {
            alert("系统异常，请稍后重试");
        }, beforeSend: function () {

        }
    });
    return list;
}
/*
*获取代码可用的数据类型列表 
*2015-7-13 
* 邢博
*返回代码可用的数据类型
*/
var GetCodeDataType = function () {
    var list = new Array();
    $.ajax({
        url: apiUrl,
        type: "Get",
        dataType: "json",
        async: false,
        data: { "sys:sno": "ETL", "sys:act": "GetCodeDataType" },
        success: function (data) {
            if (data.IsSccuess) {

                for (var i = 0; i < data.Data.Items.length; i++) {
                    list[i] = data.Data.Items[i].Name;
                }
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }

        },
        error: function () {
            alert("系统异常，请稍后重试");
        }, beforeSend: function () {

        }
    });
    return list;
}

/*
*获取数据库可用类型
*2015-7-23
* 邢博
*返回取数据库可用类型
*/
var GetDbDataType = function () {
    var list = new Array();
    $.ajax({
        url: apiUrl,
        type: "Get",
        dataType: "json",
        async: false,
        data: { "sys:sno": "ETL", "sys:act": "GetDbDataType" },
        success: function (data) {
            if (data.IsSccuess) {
                for (var i = 0; i < data.Data.Items.length; i++) {
                    list[i] = data.Data.Items[i];
                }
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }

        },
        error: function () {
            alert("系统异常，请稍后重试");
        }, beforeSend: function () {

        }
    });
    return list;
}
//调取查看  nw | n | ne | w | e | sw | s | se
var ShowToolTip = function (expresion, location) {
    if (location == undefined) {
        location = "n";
    }
    if (expresion == undefined) {
        expresion = "[data-tipsy=tipsy]";
    }
    $(expresion).tipsy({ gravity: location, title: function () { return this.getAttribute('data-title'); } });
}
//截取字符串并组合成显示标题的A标签
//注未js初始化 请调用ShowToolTip();函数
//size默认20
var SubstringForTableHtml = function (content, size) {
    var html = "";
    if (size == undefined)
        size = 40;
    if (content.length > size) {
        html = content.substring(0, size) + "...";
    } else {
        html = content;
    }
    return "<a href='javascript:void(0);' data-tipsy='tipsy' data-title='" + content + "'>" + html + "</a>";
}
//拖动容器代码
var rDrag = {
    o: null,
    init: function (o) {
        o.onmousedown = this.start;
    },
    start: function (e) {
        var o;
        e = rDrag.fixEvent(e);
        e.preventDefault && e.preventDefault();
        rDrag.o = o = this;
        o.x = e.clientX - rDrag.o.offsetLeft;
        o.y = e.clientY - rDrag.o.offsetTop;
        document.onmousemove = rDrag.move;
        document.onmouseup = rDrag.end;
    },
    move: function (e) {
        e = rDrag.fixEvent(e);
        var oLeft, oTop;
        oLeft = e.clientX - rDrag.o.x;
        oTop = e.clientY - rDrag.o.y;
        rDrag.o.style.left = oLeft + 'px';
        rDrag.o.style.top = oTop + 'px';
    },
    end: function (e) {
        e = rDrag.fixEvent(e);
        rDrag.o = document.onmousemove = document.onmouseup = null;
    },
    fixEvent: function (e) {
        if (!e) {
            e = window.event;
            e.target = e.srcElement;
            e.layerX = e.offsetX;
            e.layerY = e.offsetY;
        }
        return e;
    }
}